package com.imy.luascript.base.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.imy.luascript.config.GlideApp;
import com.imy.luascript.utils.RxUtils;
import com.imy.luascript.utils.ToolBarBuilder;
import com.imy.luascript.utils.ViewControl;
import com.imy.luascript.viewmodel.BaseImplView;
import com.imy.luascript.viewmodel.BaseViewModel;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.subscriptions.CompositeSubscription;

/**
 * @ProjectName: frame
 * @Package: com.imy.frame.activity
 * @ClassName: BaseActivity
 * @Description: 描述
 * @Author: Imy
 * @CreateDate: 2020/10/26 15:55
 */
public abstract class BaseActivity<V extends BaseViewModel> extends AppCompatActivity implements BaseImplView {
    protected final String TAG = this.getClass().getSimpleName();
    protected CompositeSubscription mCompositeSubscription;
    protected V mViewModel; //P层
    protected BaseActivity<V> self;
    protected ViewControl viewControl;
    private Unbinder unbinder;

    public static void start(Context context, Intent intent, Class<?> c) {
        intent.setClass(context, c);
        context.startActivity(intent);
    }

    public static void start(Context context, Class<?> c) {
        Intent intent = new Intent(context, c);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        self = this;
        viewControl = new ViewControl( getToolBarBuilder());
        setContentView(viewControl.getContentView(self,getLayoutID()));
        unbinder = ButterKnife.bind(this);
        mViewModel = getViewModel();
        if (isBindEventBus()) {
            EventBus.getDefault().register(this);
        }

        Intent intent = getIntent();
        if (null != intent) {
            getBundleExtras(intent);
        }

        initView();
    }

    /**
     * 获取传递过来的参数
     */
    protected void getBundleExtras(Intent intent) {
    }

    protected abstract V getViewModel();

    @Override
    protected void onResume() {
        super.onResume();
        GlideApp.with(this).resumeRequests();
    }

    @Override
    protected void onPause() {
        super.onPause();
        GlideApp.with(this).pauseRequests();
    }

    @Override
    protected void onDestroy() {
        GlideApp.get(this).clearMemory();
        if (unbinder != null) {
            unbinder.unbind();
        }
        if (this.mCompositeSubscription != null) {
            this.mCompositeSubscription.unsubscribe();
            mCompositeSubscription = null;
        }
        //清空viewmodel的缓存
        if (mViewModel!=null){
            mViewModel.onCleared();
        }
        if (isBindEventBus()){
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();

    }


    // 修复app被系统回收以后再重新进入，fragment数据空白的问题
    @SuppressLint("MissingSuperCall")
    @Override
    public void onSaveInstanceState(Bundle outState) {

    }

    /**
     * 是否需要绑定EventBus
     */
    protected boolean isBindEventBus() {
        return false;
    }

    protected abstract void initView();

    protected abstract int getLayoutID();

    protected abstract ToolBarBuilder getToolBarBuilder();
}